<template>
  <el-input v-model="searchText" placeholder="请输入职位id或昵称" style="width: 200px; margin-right: 10px" />
  <el-button @click="handleClick" type="primary">
    <el-icon style="vertical-align: middle">
      <Search />
    </el-icon>
    <span style="vertical-align: middle"> Search </span>
  </el-button>

  <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="jobId" label="职位ID" width="200" />
    <el-table-column prop="jobTitle" label="职位昵称" width="200" />
    <el-table-column prop="salary" label="职位薪资" width="200" />
    <el-table-column prop="hr" label="HR昵称" width="200" />
    <el-table-column prop="location" label="职位地址" width="200" />
    <el-table-column fixed="right" label="Operations" min-width="200">
      <template #default="scope">
        <el-button @click="detail(scope.row.jobId)" link type="primary" size="small">
          详细信息
        </el-button>
          <!-- 其他列的内容 -->
        <el-button
            :disabled="isButtonDisabled(scope.row.deleted)"
            @click="scope.row.deleted !== 2 && open(scope.row.jobId)"
            link
            type="danger"
            size="small"
        >
          {{ getButtonText(scope.row.deleted) }}
        </el-button>

      </template>
    </el-table-column>
  </el-table>

  <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[10, 15, 20, 25, 30]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px;

              justify-content: flex-end" />

</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, computed } from 'vue'
import {getAllJobsService, UpdateJobStatusService} from "@/api/job.js";
import { ElMessage, ElMessageBox } from 'element-plus'
import {updateUserService} from "@/api/user.js";
import {useRouter} from "vue-router";

const router = useRouter();
const tableData = ref([{
  jobId: '123456',
  jobTitle: '姓名',
  salary: '11-12k',
  hr: 'sda',
  location: 'ddsadasjhgha',
  deleted: 1
}])

// 计算属性或方法来决定按钮文案
const getButtonText = (deletedStatus) => {
  return deletedStatus === 1 ? '已通过' : '审核岗位';
};

// 方法或计算属性来判断按钮是否应被禁用
const isButtonDisabled = (deletedStatus) => {
  return deletedStatus === 1;
};

const pageNum = ref(1)
const total = ref(20)
const pageSize = ref(15)
const searchText = ref('')

const searchForm = ref({
  jobId: '',
  jobTitle: '',
  pageNum: pageNum.value,
  pageSize: pageSize.value
});

async function handleClick() {
  //判断输入的是否为数字
  if(searchText.value !== ''){
    if(!isNaN(searchText.value)) {
      searchForm.value.jobId = searchText.value
    }else {
      searchForm.value.jobTitle = searchText.value
    }
  }else {
    searchForm.value.jobId = ''
    searchForm.value.jobTitle = ''
  }
  searchForm.value.pageNum = pageNum.value
  searchForm.value.pageSize = pageSize.value
  console.log(searchForm.value)
  let result = await getAllJobsService(searchForm.value);
  tableData.value = result.data.item;
  total.value = result.data.total
}

//首页搜索
async function GET_ALL_USERS(){
  searchForm.value.pageNum = pageNum.value
  searchForm.value.pageSize = pageSize.value
  let result = await getAllJobsService(searchForm.value);
  tableData.value = result.data.item;
  total.value = result.data.total
}

GET_ALL_USERS()

const onSizeChange = (size) => {
  pageSize.value = size
  GET_ALL_USERS()
}

const onCurrentChange = (num) => {
  pageNum.value = num;
  GET_ALL_USERS()
}

const detail = (jobId) => {
  router.push(`/JobDetails/${jobId}`)
}

const open = async (jobId) => {
  ElMessageBox.confirm(
      '该职位是否通过审核?',
      'Warning',
      {
        confirmButtonText: '通过',
        cancelButtonText: '删除',
        type: 'warning',
        distinguishCancelAndClose: true, // 区分取消和关闭按钮
      }
  )
      .then(() => {
        console.log('jobId', jobId)
        UpdateJobStatusService(jobId, 1);
        ElMessage({
          type: 'success',
          message: '审核通过',
        })
      })
      .catch(async (action) => { // action可以帮助识别是取消还是删除
        if (action !== 'cancel') {
          // 用户点击了"取消审核"，可能不需要额外操作，直接返回
          return;
        }
        // 用户点击了"删除"
        await UpdateJobStatusService(jobId, 2);
        ElMessage({
          type: 'danger',
          message: '删除成功',
        });
      })
      .finally(() => {
        // 不论是通过、删除还是直接关闭对话框，都执行数据刷新
        GET_ALL_USERS();
      });
}
</script>

<style scoped>

</style>